<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var islandPerimeter = function (grid) {
        let m = grid.length
        let n = grid[0].length
        let sum = 0
        function backTracking(i, j) {
          if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
            sum++
            return
          }
          if (grid[i][j] == -1) return
          grid[i][j] = -1
          backTracking(i - 1, j)
          backTracking(i + 1, j)
          backTracking(i, j - 1)
          backTracking(i, j + 1)
        }
        for (let i = 0; i < m; i++) {
          for (let j = 0; j < n; j++) {
            if (grid[i][j] == 1) {
              backTracking(i, j)
            }
          }
        }
        return sum
      }
    </script>
  </body>
</html>
